<template>
    <div class="wrapper">
        <div class="top">
            <div class="dateMon" @click="openPicker">
                <span>{{ viewDate }}</span>
                <img src="@/assets/chevron-down@2x.png" alt="" />
            </div>
            <div id="myChart" :style="{ width: '100%', height: '250px' }"></div>
        </div>
        <div class="bottom">
            <div class="dateMon" @click="showSheet = true">
                <span>{{ typeName }}</span>
                <img src="@/assets/chevron-down@2x.png" alt="" />
            </div>
            <div class="actCodeList">
                <empty v-if="!actCodeList.length"></empty>
                <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了">
                    <div class="item" v-for="item in actCodeList" :key="item">
                        <div class="con">
                            <div class="left">
                                <img src="@/assets/agentImg.png" class="agentImg" alt="" />
                            </div>
                            <div class="right">
                                <div class="name">{{ item.agentName }}</div>
                                <div class="phone">{{ item.phoneNo }}</div>
                                <div class="num">
                                    <div>已绑定：{{ item.bindNum }}</div>
                                    <div>已唤醒：{{ item.actNum }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </van-list>
            </div>
        </div>
        <van-popup v-model:show="show" round position="bottom">
            <van-tabs v-model:active="active" color="#66d6ff">
                <van-tab title="按月">
                    <van-picker title="标题" @change="onChangeMon" :columns="monList" :show-toolbar="false" />
                </van-tab>
                <van-tab title="按日">
                    <van-picker :columns="dateList" @change="onChangeDate" :show-toolbar="false" />
                </van-tab>
            </van-tabs>
            <div class="btn" @click="endDate">确定</div>
        </van-popup>
        <van-action-sheet v-model:show="showSheet" :actions="typeList" cancel-text="取消" close-on-click-action
            @select="selectType" />
    </div>
</template>

<script>
import * as echarts from 'echarts'
import {
    appRankStatisTerminalDayAndMonth,
    appStatisTerminalDayAndMonth,
    findByTargetAndClassesId,
    queryAllTimeRecently
} from '@/api/dataAnalysis'
export default {
    data() {
        return {
            chartData: [],
            viewDate: '',
            show: false,
            loading: false,
            finished: true,
            active: 0,
            monList: [],
            monL: '',
            dateL: '',
            myChart: null,
            typeName: '',
            showSheet: false,
            actCode: '',
            typeList: [],
            actCodeList: [],
            dateList: []
        }
    },
    methods: {
        onChangeMon(e) {
            console.log(e)
            this.monL = e.selectedValues[0]
        },
        onChangeDate(e) {
            console.log(e)
            this.dateL = e.selectedValues[0] + '-' + e.selectedValues[1]
        },
        selectType(action) {
            console.log(action, 123)
            this.typeName = action.name
            this.actCode = action.id
            this.actCodeList = []
            this.getAppRankStatisTerminalDayAndMonth()
        },
        endDate() {
            console.log(this.dateL)
            if (this.active === 0) {
                this.viewDate = this.monL
            } else {
                this.viewDate = this.dateL
            }
            this.actCodeList = []

            this.getAppStatisTerminalDayAndMonth()
            this.getAppRankStatisTerminalDayAndMonth()

            this.show = false
        },
        getQueryAllTimeRecently() {
            queryAllTimeRecently().then(res => {
                this.monList = res.object.map(item => {
                    for (const key in item) {
                        return { text: key, value: key }
                    }
                })
                console.log(res.object, 11111)
                this.dateList = res.object.map(item => {
                    for (const key in item) {
                        item[key].reverse()
                        let aqa = item[key].map(item11 => {
                            return { text: item11, value: item11 }
                        })
                        return { text: key, value: key, children: aqa }
                    }
                })
                console.log(this.dateList, 22222)
                this.viewDate = this.monList[0].text
                this.getAppStatisTerminalDayAndMonth()
            })
        },
        getAppRankStatisTerminalDayAndMonth() {
            appRankStatisTerminalDayAndMonth({
                searchDate: this.viewDate,
                currentPage: 1,
                dateType: !this.active ? 'MONTH' : 'DAY',
                actCode: this.actCode
            }).then(res => {
                this.actCodeList = res.object.list
            })
        },
        showorders(dateList, valueList) {
            // 基于准备好的dom，初始化echarts实例
            document.getElementById('myChart').setAttribute('_echarts_instance_', '')

            var chartDom = document.getElementById('myChart')
            if (this.myChart != null && this.myChart != '' && this.myChart != undefined) {
                this.myChart.dispose() //销毁
            }
            this.myChart = echarts.init(chartDom)
            // 指定图表的配置项和数据
            var option

            option = {
                xAxis: {
                    type: 'category',
                    data: dateList
                },

                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: valueList,
                        type: 'line',
                        smooth: true,
                        itemStyle: {
                            normal: {
                                color: '#eb3941', //改变折线点的颜色
                                lineStyle: {
                                    color: '#eb3941' //改变折线颜色
                                }
                            }
                        },
                    },

                ]
            }
            option && this.myChart.setOption(option)
        },
        openPicker() {
            this.show = true
            this.monL = this.monList[0].text
            console.log(this.dateList[0])
            this.dateL = this.dateList[0].text + '-' + this.dateList[0].children[0].text
        },
        getAppStatisTerminalDayAndMonth() {
            appStatisTerminalDayAndMonth({
                searchDate: this.viewDate,
                currentPage: 1,
                dateType: !this.active ? 'MONTH' : 'DAY'
            }).then(res => {
                this.chartData = res.object.list
                const dateList = res.object.list.map(function (item) {
                    return item.statisDate
                })
                const valueList = res.object.list.map(function (item) {
                    return item.actNum
                })
                this.showorders(dateList, valueList)
            })
        },
        getFindByTargetAndClassesId() {
            findByTargetAndClassesId().then(res => {
                this.typeList = [{ id: '', name: '全部' }, ...res.object]
                this.typeName = this.typeList[0].name
                this.getAppRankStatisTerminalDayAndMonth()
            })
        }
    },
    mounted() {
        this.getQueryAllTimeRecently()
        this.getFindByTargetAndClassesId()

    }
}
</script>

<style scoped lang="scss">
.wrapper {
    padding: 10px;

    .top,
    .bottom {
        background-color: #ffffff;
        padding: 10px;
        border-radius: 10px;

        .dateMon {
            font-size: 16px;
            font-weight: 400;
            color: #000000;
            opacity: 1;
            display: flex;
            align-items: center;

            img {
                width: 20px;
            }
        }

        .con {
            display: flex;
            align-items: center;
            padding: 10px;
            border-bottom: 1px solid var(--van-cell-border-color);

            .left {
                .agentImg {
                    width: 50px;
                }
            }

            .right {
                flex: 1;
                padding: 0 10px;
                line-height: 25px;

                .name {
                    font-size: 14px;
                }

                .phone {
                    font-size: 12px;
                    color: #9a9a9a;
                }

                .num {
                    font-size: 12px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                }
            }
        }

        .con:last-child {
            border: none;
        }
    }

    .bottom {
        margin-top: 10px;
    }
}

.btn {
    width: 159px;
    height: 40px;
    background: #66d6ff;
    opacity: 1;
    border-radius: 6px;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 400;
    color: #ffffff;
    margin: 20px auto;
}
</style>
